package com.example.graduatetest.view.model;

import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.databinding.ActivityUploadScoreBinding;
import com.example.graduatetest.model.ScoreModel;

public class UploadScoreViewModel extends ViewModel {
    private Activity activity;
    private ActivityUploadScoreBinding binding;
    private String class_name;
    private String class_number_send;
    private Handler handler = new Handler();
    private MutableLiveData<String> class_number;
    private MutableLiveData<String> student_number;
    private MutableLiveData<String> test_name;
    private MutableLiveData<Boolean> isHundred;
    private MutableLiveData<String> score;

    public void init(Activity activity,ActivityUploadScoreBinding binding,String class_name,String class_number_send){
        this.activity = activity;
        this.binding = binding;
        this.class_name = class_name;
        this.class_number_send = class_number_send;
        if (class_number == null) class_number = new MutableLiveData<>();
        if(student_number == null) student_number = new MutableLiveData<>();
        if (test_name == null) test_name = new MutableLiveData<>();
        if (isHundred == null) isHundred = new MutableLiveData<>();
        if(score == null) score = new MediatorLiveData<>();
        isHundred.setValue(true);
        class_number.setValue(class_number_send);
        toolbarInit();
        initChick();
    }

    //初始toolbar
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar11;
        toolbar.setTitle("成绩上传");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });

    }

    //初始化点击事件
    private void initChick(){
        //editText不可编辑
        binding.edClassNumber.setFocusableInTouchMode(false);
        //上传并退出代码逻辑
        binding.uploadExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(class_number.getValue() != null && student_number.getValue() != null
                && test_name.getValue() != null && score.getValue() != null && isHundred.getValue() != null){
                    ScoreModel.insertOneScore(class_number.getValue(), student_number.getValue(), test_name.getValue(), score.getValue(), class_name, isHundred.getValue(), new StringCallBack() {
                        @Override
                        public void onSuccess(String result) {
                            if(result.equals("success")){
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(activity, "上传成功", Toast.LENGTH_SHORT).show();
                                        activity.onBackPressed();
                                    }
                                });
                            }else{
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(activity, "已存在", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }

                        @Override
                        public void onFailed() {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(activity, "上传失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                }else{
                    Toast.makeText(activity, "信息不完整,无法上传", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //上传且执行下一个
        binding.uploadNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(class_number.getValue() != null && student_number.getValue() != null
                        && test_name.getValue() != null && score.getValue() != null && isHundred.getValue() != null){
                    ScoreModel.insertOneScore(class_number.getValue(), student_number.getValue(), test_name.getValue(), score.getValue(), class_name, isHundred.getValue(), new StringCallBack() {
                        @Override
                        public void onSuccess(String result) {
                            if(result.equals("success")){
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(activity, "上传成功", Toast.LENGTH_SHORT).show();
                                        student_number.setValue("");
                                        score.setValue("");
                                    }
                                });
                            }else {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(activity, "已存在", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }

                        @Override
                        public void onFailed() {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(activity, "上传失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                }else{
                    Toast.makeText(activity, "信息不完整,无法上传", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public MutableLiveData<String> getClass_number() {
        return class_number;
    }

    public MutableLiveData<String> getStudent_number() {
        return student_number;
    }

    public MutableLiveData<String> getTest_name() {
        return test_name;
    }

    public MutableLiveData<Boolean> getIsHundred() {
        return isHundred;
    }

    public MutableLiveData<String> getScore() {
        return score;
    }
}
